﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FizzBuzz;

namespace FizzBuzzConsole
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int number1;
            int number2;
            int destination;
            
            Console.WriteLine("Enter start number");

            var startnumber = Console.ReadLine();

            Console.WriteLine("Enter end number");

            var endnumber = Console.ReadLine();

            Console.WriteLine("Select the destination. Press 1 for Screen or 2 for File.");

            bool result = int.TryParse(Console.ReadLine(), out destination);
          
            if (!int.TryParse(startnumber, out number1))
            {
                Console.WriteLine("Please enter valid number for 1st input.");
                Console.ReadLine();
                return;
            }

            if (!int.TryParse(endnumber, out number2))
            {
                Console.WriteLine("Please enter valid number for 2nd input.");
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Enter Fizz token <optional>");

            var fizztoken = Console.ReadLine();

            Console.WriteLine("Enter Buzz token <optional>");

            var buzztoken = Console.ReadLine();

            var fizzbuzzresult = FizzBuzzutility.GenerateFizzBuzz(number1, number2, string.IsNullOrEmpty(fizztoken) ? "Fizz" : fizztoken, 
                string.IsNullOrEmpty(buzztoken) ? "Buzz" : buzztoken);

            if (destination == 1)
            {
                foreach (var fizzbuzz in fizzbuzzresult)
                {
                    Console.WriteLine(fizzbuzz);
                }
            }
            else
            {
                using (FileStream filestream = new FileStream(@"C:\out.txt", FileMode.Create))
                {
                    var streamwriter = new StreamWriter(filestream);
                    streamwriter.AutoFlush = true;
                    Console.SetOut(streamwriter);

                    foreach (var fizzbuzz in fizzbuzzresult)
                    {
                        Console.WriteLine(fizzbuzz);
                    }
                }
                
            }
            Console.ReadLine();
        }
    }
}
